package com.wei.frame.ui.imageloader;

import android.content.Context;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.wei.frame.utils.Preconditions;

import javax.inject.Inject;
import javax.inject.Singleton;

/**
 * 作者：赵若位
 * 时间：2020/9/28 17:23
 * 邮箱：1070138445@qq.com
 * 功能：
 */
@Singleton
public final class ImageLoader implements IImageLoader
{
    @Inject
    public ImageLoader()
    {

    }

    /*需要对传入的Context进行判空处理~*/
    private void checkNotNull(Context context)
    {
        Preconditions.checkNotNull(context, "The Context of ImageLoader is null~");
    }


    /*暂停Glide加载*/
    public void pause(@Nullable Context context)
    {
        checkNotNull(context);
        GlideApp.with(context)
                .pauseRequestsRecursive();
    }

    /*继续Glide加载*/
    public void resume(@Nullable Context context)
    {
        checkNotNull(context);
        GlideApp.with(context)
                .resumeRequestsRecursive();
    }

    /*清除硬盘缓存*/
    public void clearDiskCache(@Nullable Context context)
    {
        checkNotNull(context);
        GlideApp.get(context)
                .clearDiskCache();
    }

    /*清除内存缓存*/
    public void clearMemory(@Nullable Context context)
    {
        checkNotNull(context);
        Glide.get(context)
                .clearMemory();
    }

    public void onTrimMemory(@Nullable Context context, int level)
    {
        GlideApp.get(context)
                .onTrimMemory(level);
    }


    public void onLowMemory(Context context)
    {
        GlideApp.get(context)
                .onLowMemory();
    }

    @Override
    public void loadImageByUrl(@Nullable Context context, @Nullable String url, @Nullable ImageView imageView)
    {
        checkNotNull(context);
        GlideApp.with(context)
                .load(url)
                .into(imageView);
    }

}
